package com.example.administrator.stulist;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IonDelListener{
    private ListView stulistview;
    private ArrayList<Student> students=new ArrayList<Student>();
    private ArrayList<Student> students_search=new ArrayList<Student>();
    private StuAdapter adapter;
    private Button add;
    private EditText edtKeyword;
    private Button btnsearch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inniStuList();
        stulistview=findViewById(R.id.studentList);
        adapter=new StuAdapter(MainActivity.this,students,this);
        stulistview.setAdapter(adapter);
        add=findViewById(R.id.btn_add);
        edtKeyword=findViewById(R.id.text_search);
        btnsearch=findViewById(R.id.btn_search);
        btnsearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyWord=edtKeyword.getText().toString().trim();
                search(keyWord,students_search);
                adapter.changedata(students_search);
            }
        });


        stulistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student s=students.get(i);
                Intent intent  =new Intent();
                intent.setClass(MainActivity.this,EdtStudent.class);
                intent.putExtra("imgid",s.getImgid());
                intent.putExtra("name",s.getName());
                intent.putExtra("age",s.getAge());
                intent.putExtra("student",i);
                startActivityForResult(intent,2001);

            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,Add_Activity.class);
                startActivityForResult(intent,1001);
            }
        });

    }

    private void search(String keyWord, ArrayList<Student> students_search) {
        students_search.clear();
        for (int i=0; i<students.size();i++){
            Student s=students.get(i);
            if(s.getName().contains(keyWord)){
                students_search.add(s);
                continue;
            }
            else if(String.valueOf(s.getAge()).contains(keyWord)){
                students_search.add(s);
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(data==null){
          return;
      }
        if (requestCode==1001){
      String name=data.getStringExtra("name");
      int age=data.getIntExtra("age",0);
      int imgid=data.getIntExtra("imgid",R.drawable.dog1);
      Student s=new Student(imgid,name,age);
      students.add(s);
      adapter.notifyDataSetChanged();
      }
      if (resultCode==2001){
          String name=data.getStringExtra("name");
          int age=data.getIntExtra("age",0);
          int imgid=data.getIntExtra("imgid",R.drawable.dog1);
          int i=data.getIntExtra("student",1);
          Student s=students.get(i);
          s.setAge(age);
          s.setName(name);
          s.setImgid(imgid);
        adapter.notifyDataSetChanged();

      }

    }

    private void inniStuList() {
        Student s1=new Student(R.drawable.dog1,"张三",18);
        Student s2=new Student(R.drawable.dog2,"张四",18);
        Student s3=new Student(R.drawable.dog3,"张五",18);
        Student s4=new Student(R.drawable.dog4,"张六",18);
        Student s5=new Student(R.drawable.dog5,"张七",18);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
    }

    @Override
    public void del(int i) {
        students.remove(i);
        adapter.notifyDataSetChanged();
    }
}
